﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Wicresoft.EKA.NRemedy;

namespace Wicresoft.EKA.MVCExtensions
{
    public class BaseController : Controller
    {
        public Dictionary<String, String> UserPreferences { get; private set; }
        public ICollection<String> Roles { get; private set; }

        public virtual IUserInfo UserInfo { get; set; }

        //protected bool IsAlternative = false;
        //protected string NowAlternativer = string.Empty;
        //protected ICollection<string> Alternativers { get; private set; }
        //protected string ErrorAddress    { get; set; }

        public String UserName
        {
            get
            {
                //if (null == Alternativers || 0 == Alternativers.Count) return User.Identity.Name;
                //if (IsAlternative) return NowAlternativer;
                //NowAlternativer = Alternative.Get(User.Identity.Name);
                //if (!string.IsNullOrWhiteSpace(NowAlternativer))
                //{
                //    IsAlternative = true;
                //    if (!ARLoginContextPool.Instance.ContainsKey(NowAlternativer))
                //        ARLoginContextPool.Instance.Add(NowAlternativer, new ARLoginContext(ConfigurationManager.AppSettings["ARServer"],
                //            NowAlternativer,
                //            ConfigurationManager.AppSettings["UnityPassword"]));
                //    return NowAlternativer;
                //}
                return User.Identity.Name;
            }
        }

        internal void OnBeforeAuthorize()
        {
            ((IBase)UserInfo).UserIdentify = UserName;
            //Alternativers = UserInfo.GetUserAlternativers(User.Identity.Name);
            Roles = UserInfo.GetUserInRolesByUserIdentify(UserName);
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            ((IBase)UserInfo).UserIdentify = UserName;
            UserPreferences = UserInfo.GetUserPreferenceByUserIdentify(UserName);
            base.OnActionExecuting(filterContext);
        }

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)UserInfo).Dispose();
            base.Dispose(disposing);
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //var status=filterContext.Result as HttpStatusCodeResult;
            //if (status != null && status.StatusCode == (int)System.Net.HttpStatusCode.Forbidden) 
            //    Redirect(string.IsNullOrWhiteSpace(ErrorAddress)?"~/Info/NoPerssion.html":ErrorAddress);

            if (Roles == null) OnBeforeAuthorize();
            ViewBag.Roles = Roles;
            ViewBag.UserPreferences = UserPreferences;

            //if (IsAlternative)
            //    UserInfo.Log("Alternative", User.Identity.Name + " Delegate " + UserName +
            //        " Visit " + filterContext.HttpContext.Request.Url.ToString(), DateTime.Now);

            base.OnActionExecuted(filterContext);
        }

    }
}
